/**
 * All rights reserved by www.xandroid.cn
 */
package xfok.net.xandroid.examples;

import xfok.net.xandroid.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * @author YangHua
 *  转载请注明出处：http://www.xfok.net/android/2010-7-1
 * 2010-7-1 www.xandroid.cn 
 */
public class ImageRotate extends Activity{
	   
	 private ImageView mImageView;
	 private Button btn1,btn2;
	 private int ScaleTimes=1,ScaleAngle=1;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.image_rotate);
	        mImageView=(ImageView)findViewById(R.id.myImageView);
	        final Bitmap bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.sample_1);
	        final int widthOrig=bmp.getWidth();
	        final int heightOrig=bmp.getHeight();
	        mImageView.setImageBitmap(bmp);
	        btn1=(Button)findViewById(R.id.myButton1);
	        btn1.setOnClickListener(new OnClickListener(){
	         public void onClick(View v){
	          ScaleAngle--;
	          if(ScaleAngle<-60){
	           ScaleAngle=-60;
	          }
	          int newWidth=widthOrig*ScaleTimes;
	          int newHeight=heightOrig*ScaleTimes;
	          float scaleWidth=((float)newWidth)/widthOrig;
	          float scaleHeight=((float)newHeight)/heightOrig;
	          Matrix matrix=new Matrix();
	          matrix.postScale(scaleWidth, scaleHeight);
	          matrix.setRotate(5*ScaleAngle);
	          Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, widthOrig, heightOrig, matrix, true);
	          BitmapDrawable myNewBitmapDrawable=new BitmapDrawable(resizeBitmap);
	          mImageView.setImageDrawable(myNewBitmapDrawable);
	         }
	        });
	        btn2=(Button)findViewById(R.id.myButton2);
	        btn2.setOnClickListener(new OnClickListener(){
	         public void onClick(View v){
	          ScaleAngle++;
	          if(ScaleAngle>60){
	           ScaleAngle=60;
	          }
	          int newWidth=widthOrig*ScaleTimes;
	          int newHeight=heightOrig*ScaleTimes;
	          float scaleWidth=((float)newWidth)/widthOrig;
	          float scaleHeight=((float)newHeight)/heightOrig;
	          Matrix matrix=new Matrix();
	          matrix.postScale(scaleWidth, scaleHeight);
	          matrix.setRotate(5*ScaleAngle);
	          Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, widthOrig, heightOrig, matrix, true);
	          BitmapDrawable myNewBitmapDrawable=new BitmapDrawable(resizeBitmap);
	          mImageView.setImageDrawable(myNewBitmapDrawable);
	         }
	        });
	    }
}
